/**
 * 
 */
package service.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import dao.CommentDAO;
import dao.impl.CommentDAOImpl;
import po.Comment;
import service.CommentService;
import util.DBUtil;

/**
 * @author曹桢瑞
 */
public class CommentServiceImpl implements CommentService {

	@Override
	public List<Comment> getCommentInfoByUserId(String userId, int num) {
		Connection con=null;
		ArrayList<Comment> list=new ArrayList<Comment>();
		try {
			con=DBUtil.getConnection();
			CommentDAO commentDAO=new CommentDAOImpl(con);
			list=commentDAO.getCommentByUserId(userId, num);
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return list;
	}

	@Override
	public List<Comment> getCommentInfoByBookId(String bookId, int num) {
		Connection con=null;
		ArrayList<Comment> list=new ArrayList<Comment>();
		try {
			con=DBUtil.getConnection();
			CommentDAO commentDAO=new CommentDAOImpl(con);
			list=commentDAO.getCommentByBookId(bookId, num);
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return list;
	}

	@Override
	public boolean addComment(Comment comment) {
		Connection con=null;
		boolean flag=false;
		try {
			con=DBUtil.getConnection();
			CommentDAO commentDAO=new CommentDAOImpl(con);
			flag=commentDAO.addComment(comment);
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return flag;
	}

	@Override
	public boolean removeComment(Comment comment) {
		Connection con=null;
		boolean flag=false;
		try {
			con=DBUtil.getConnection();
			CommentDAO commentDAO=new CommentDAOImpl(con);
			flag=commentDAO.removeCommentByIds(comment.getUserId(), comment.getBookId());
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return flag;
	}

	@Override
	public boolean updateComment(Comment comment) {
		Connection con=null;
		boolean flag=false;
		try {
			con=DBUtil.getConnection();
			CommentDAO commentDAO=new CommentDAOImpl(con);
			flag=commentDAO.updateComment(comment);
		}
		catch(Exception e) {
			System.out.println(e);
		}
		return flag;
	}

}
